﻿var links=top.location.href.substring(7).split("/");
var basePath="http://"+links[0]+"/"+links[1]+"/";
$(function () {

    $(".recon").each(function () {
    	this.innerHTML = formatFeedback(this.innerHTML.replace("\"", "&quot;"));
    });
//    $(".del").click(function () {
//        return confirm("评论一旦删除，不可恢复。确定要删除吗？");
//    });
});
var ids = "";
var reply_box = null;
function doReply(e) {
    ids = e.lang;
    
    var td = $(e).parent().parent().next().children().eq(0);
    if (!reply_box) {
        reply_box = document.createElement("div");
        reply_box.id = "reply_box";
        var s = '<fieldset style="padding:6px;"><legend>回复评论</legend>'
        + '<form action="?reply=1" method="post">'
        + '<textarea id="txtReply" name="con" cols="90" rows="8"></textarea>'
        + '<p style="padding-top:6px;">'
        + '<input type="submit" class="t_btn" value="提交回复" onclick="saveRe()" />&nbsp;&nbsp;'
        + '<input type="button" class="t_btn btngray" value="取消" onclick="cancelRe()" />&nbsp;&nbsp;'
        + '<span id="sp_note" style="color:#f00;"></span>'
        + '<input id="ids" name="ids" type="hidden" />'
        + '</p></form></fieldset>';
        reply_box.innerHTML = s;
        td.append($(reply_box));
        initXhEditor();
    } else {
        $(reply_box).remove();
        td.append($(reply_box));
    }
    $("#txtReply").val("");
    $("#txtReply").focus();
}
function saveRe() {
    if (!ids) return false;
    if (!csdn.hasVal("txtReply")) {
        $("#sp_note").html("请输入回复内容。");
        $("#txtReply").focus();
        return false;
    }
    $("#ids").val(ids);
}
function cancelRe() {
    $(reply_box).remove();
}


//初始化编辑器
function initXhEditor() {
    $('#txtReply').xheditor({
        tools: 'Bold,Italic,Underline,Strikethrough,|,Emot',
        showBlocktag: false, forcePtag: false, beforeSetSource: ubb2html, beforeGetSource: html2ubb
    });
}
function formatFeedback(content) {
    content = $.trim(content);

    var re = /\[code=([\w#\.]+)\]([\s\S]*?)\[\/code\]/ig;

    var codelist = [];
    while ((mc = re.exec(content)) != null) {
        codelist.push(mc[0]);
        content = content.replace(mc[0], "--code--");
    }
    content = formatQuote(content);
    content = content.replace(/\[e(\d+)\]/g, "<img src=\"http://static.blog.csdn.net/images/emotions/e$1.gif\"\/>");
    content = content.replace(/\[reply]([\s\S]*?)\[\/reply\]/gi, '<p>回复<a href="'+basePath+'learning/personal.jsp?userNickName=$1" class="user_name" target=_blank>$1</a>:<br /></p>');
    content = content.replace(/\[url=([^\]]+)]([\s\S]*?)\[\/url\]/gi, '<a href="$1" target="_blank">$2</a>');
    content = content.replace(/\[img(=([^\]]+))?]([\s\S]*?)\[\/img\]/gi, '<img src="$3" style="max-width:400px;max-height:200px;" border="0" title="$2" />');
    content = content.replace(/\r?\n/ig, "<br />");

    if (codelist.length > 0) {
        var re1 = /--code--/ig;
        var i = 0;
        while ((mc = re1.exec(content)) != null) {
            content = content.replace(mc[0], codelist[i]);
            i++;
        }
    }
    content = content.replace(/\[code=([\w#\.]+)\]([\s\S]*?)\[\/code\]/ig, function (all, s1, s2) {
        return '<pre name="code" class="' + s1 + '">' + (s2.replace(/(^<br>)|(<br>$)/ig, '')) + '</pre>';
    });
    return content;
}
function formatQuote(str) {
    var m = /\[quote=([^\]]+)]([\s\S]*)\[\/quote\]/gi.exec(str);
    if (m) {
        return '<fieldset style="padding:8px"><legend>引用“<a href="'+basePath+'learning/personal.jsp?userNickName='+m[1]+'" class="user_name" target=_blank>' + m[1] + '</a>”的评论：</legend>' + formatQuote(m[2]) + '</fieldset>';
    } else {
        return str;
    }
}